package com.dianrui.fmi.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dianrui.fmi.Constant;
import com.dianrui.fmi.util.SPUtils;
import com.dianrui.fmi.util.StringUtils;

import java.util.Locale;

import butterknife.ButterKnife;

public abstract class BaseFragment extends Fragment {
    protected Activity mActivity;
    protected LayoutInflater mInflater;
    protected View rootView;
    public SPUtils spUtils;
    public boolean firstLoad = false;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(getContentViewId(), container, false);
        spUtils = new SPUtils(getActivity(), Constant.cfgFileName, Context.MODE_PRIVATE);
        ButterKnife.bind(this, rootView);
        initView();
        firstLoad = true;//视图创建完成，将变量置为true
        getData();
        selectLanguage();
        return rootView;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        getData();

    }

    public void selectLanguage() {
        //设置语言类型
        Resources resources = getResources();
        Configuration configuration = resources.getConfiguration();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        if (!StringUtils.isEmpty(spUtils.getString("language"))) {
            switch (spUtils.getString("language")) {
                case "english":
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                        configuration.setLocale(Locale.ENGLISH);//英语
                    }

                    break;
                case "burma":
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                        Locale myLocale = new Locale("my", "rMM");
                        configuration.setLocale(myLocale);//缅甸语
                    }
                    break;


                default:
                    break;
            }

            resources.updateConfiguration(configuration, displayMetrics);
        }

    }

    public abstract int getContentViewId();//布局ID

    public abstract void initView();

    public abstract void getData();


    protected void JumpActivitys(Class<?> clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        startActivity(intent);
    }

    protected void JumpKillActivitys(Class<?> clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        startActivity(intent);
        getActivity().finish();
    }


    @Override
    public void onDestroy() {
        firstLoad = false;//视图销毁将变量置为false
        super.onDestroy();
    }
}
